export default class extends think.Service {
    constructor() {
        super();
        this.areaModel = this.db['sys_area'];
    }

    /**
     * 树形结构化返回区域数据
     * @returns {Promise<*>}
     */
    async getAreaList() {
        const Op = this.db.sequelize.Op;

        const areaList = await this.areaModel.findAll({
            where: {
                parent_ids: {
                    [Op.like]: '%0,1,2580,%'//云南省
                }
            }, raw: true
        });
        if (areaList.length > 0) {
            return new this.TreeService().transData(areaList);
        }
        return null;
    }


    /**
     * 获取区域数据
     */
    async getAllArea() {
        const Op = this.db.sequelize.Op;
        const where = {
            parent_ids: {
                [Op.like]: '%0,1,2580,%'//云南省
            }
        };
        return this.areaModel.findAll({where: where, raw: true});

    }

    /**
     * 根据area_code查询区域
     */
    async queryAreaByAreaCode(area_code) {
        const where = {area_code: area_code};
        try {
            let result = await this
                .areaModel
                .findAll({where: where});
            return result
        } catch (error) {
            return this
                .exceptionService
                .handleError(error);
        }
    }


    /**
     * 根据id
     * 查询区域
     */
    async queryAreaById(id) {
        const where = {id: id};
        try {
            let result = await this
                .areaModel
                .findAll({where: where});
            return result
        } catch (error) {
            return this
                .exceptionService
                .handleError(error);
        }
    }
}